﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SampleVisualCtr : MonoBehaviour {

    public GameObject _quadObject;
    public float sampleMultiper = 1;
    float[] samples;
    List<BarCtr> bars = new List<BarCtr>();
    public int decreaseSampleRadio = 1;    //降采样倍数，2即采样数量为  samples.length / 2


    public float radius = 1;

	// Use this for initialization
	void Start () {
        samples = MainController.Instance._musicCore.samples;
        int quadCount = samples.Length / decreaseSampleRadio;
        float per_angle = 360f / quadCount;
        for (int i = 0; i < quadCount; i++)
        {
            GameObject obj = Instantiate<GameObject>(_quadObject);
            Vector3 dir = Quaternion.Euler(0, per_angle * i, 0) * transform.right;
            obj.SetActive(true);
            obj.transform.SetParent(transform);
            obj.transform.position = transform.position + dir * radius;
            obj.transform.LookAt(transform.position + dir * radius * 2);
            bars.Add(obj.GetComponent<BarCtr>());
        }

    }
	
	// Update is called once per frame
	void Update () {
        int barIndex = 0;
        for (int i = 0; i < samples.Length; i+=decreaseSampleRadio)
        {
               bars[barIndex].sampleValue = samples[i] * sampleMultiper * i * 0.1f;
            barIndex++;
        }
    }
}
